/**
 * Created by Administrator on 2018/11/23 0023.
 */
import axios from 'axios'
import router from '../../src/router'
import {getStore, setStore, removeStore} from './storage'
import {Toast} from 'we-vue'
// 统一请求路径前缀
// 超时设定
axios.defaults.timeout = 30000;
axios.interceptors.request.use(config => {
  Toast.loading({
    duration: 30000,
    message: '请求中'
  });
  return config;
}, err => {
  Toast.close();
  Toast.fail('请求超时');
  return Promise.resolve(err);
});

// http response 拦截器
axios.interceptors.response.use(response => {
  Toast.close();
  console.log(response);
  const data = response.data;
  // 根据返回的code值来做不同的处理(和后端约定)
  switch (data.code) {
    case 102:
      // 未登录 清除已登录状态
      router.push('/');
      removeStore("token");
      break;
    case 103:
      // 账户冻结
      Toast.fail(data.info);
      removeStore("token");
      break;
    case 400:
      // 没有权限
      router.push('/');
      break;
    case 403:
      if (data.message !== null) {
        Toast.fail(data.message)
      } else {
        Toast.fail("未知错误")
      }
      router.push('error');
      break;
    case 500:
      // 错误
      if (data.message !== null) {
        Toast.fail(data.message);
      } else {
        Toast.fail("未知错误");
      }
      router.push('error');
      break;
    default:
      return data;
  }

  return data;
}, (err) => {
  // 返回状态码不为200时候的错误处理
  Toast.fail(err.toString());
  return Promise.resolve(err);
});

export const getRequest = (url, params) => {
  let token = getStore('token');
  return axios({
    method: 'get',
    url: `${url}`,
    params: params,
    headers: {
      'token': token
    }
  });
};

export const postRequest = (url, params) => {
  let token = getStore("token");
  return axios({
    method: 'post',
    url: `${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = '';
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
      }
      return ret;
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      'token': token
    }
  });
};

export const putRequest = (url, params) => {
  let token = getStore("token");
  return axios({
    method: 'put',
    url: `${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = '';
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
      }
      return ret;
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      'token': token
    }
  });
};

export const deleteRequest = (url, params) => {
  let token = getStore('token');
  return axios({
    method: 'delete',
    url: `${url}`,
    params: params,
    headers: {
      'token': token
    }
  });
};

export const uploadFileRequest = (url, params) => {
  let token = getStore('token');
  return axios({
    method: 'post',
    url: `${url}`,
    params: params,
    headers: {
      'token': token
    }
  });
};



